# -*- coding: utf-8 -*-
'''
Created on Jun 25, 2013

@author: LONG HOANG GIANG
'''
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
import workerpool
import json
import re
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')


class VinaWap():
    
    _url = 'http://vinawap.mobi/stories/truyen-nguoi-lon/'
    _output = '/longhoanggiang/truyentext/_vinawaptnl'
    
    def __init__(self):
        pyLib.createIfNotExistsPath(self._output + '/files')
    
    def getNextPage(self, tNode):
        
        nextPageNode = tNode.xpath(".//div[@class='page']/*")
        if len(nextPageNode) == 0: return None
        ok = False
        url = ''
        for node in nextPageNode:
            if ok: 
                url = node.get('href')
                if url != '': break
            if node.tail == None: continue
            if node.tail.strip() == '': continue
            ok = True
        return url if url != '' else None
    
    def detectHasChapterInside(self, item):
        tree = pyLib.loadWeb(item['url']).build_tree()
        items = []
        try:
            for item in tree.xpath("//*[contains(., 'Trọn bộ')]/../../following-sibling::*[1]//li//a".decode('utf-8')):
                href = item.get('href')
                name = pyLib.stringify(item)
                items.append({'name': name, 'url': href})
            items.reverse()
        except:
            print sys.exc_info()[1]
        return items if len(items) > 0 else [item]
    
    def getDetailOfStories(self, item):
        index = 1
        data = []
        for _item in self.detectHasChapterInside(item):
            url = _item['url']
            logging.info("get detail of {0} url {1}".format(item['name'], item['url']))
            
            hasMultiChap = False
            while url != None and url != '':
                tree = pyLib.loadWeb(url).build_tree()
                
                if tree == None: break
                
                contentNode = tree.xpath("//div[@class='tin-anh']")
                fileName = pyLib.crc32unsigned(url)
                url = self.getNextPage(contentNode[0])
                if url != None and not hasMultiChap:
                    hasMultiChap = True
                    
                pyLib.Etree.cleanNode("./*[position()<4]", contentNode)
                pyLib.Etree.cleanNode(".//div[@class='page']", contentNode)
                text = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode)).strip()
                
                name = item['name']
                if hasMultiChap:
                    name = item['name'] + ' ({0})'.format(index)
                    index += 1
                
                name = re.sub(r"\[.+\]", '', name).strip()
                if name == '': raise
                name = pyLib.toUpper(name)
                text = "<b>{0}</b><br /><hr /><br />{1}".format(name, re.sub(r"\n", "<br />", text))
                print '----------------------------'
                print name, fileName
                print '----------------------------'
#                print text
#                pyLib.gz_file_put_content(self._output + "/files/" + fileName, json.dumps(text))
                
                data.append({'name': name, 'path': fileName})
        return data
                
    
    def getListStories(self, start, end):
        data = []
        # max = 77
        for page in range(start, end+1):
            url = 'http://vinawap.mobi/stories/truyen-nguoi-lon/'
            if page > 1:
                url = 'http://vinawap.mobi/stories/truyen-nguoi-lon/page/{0}'.format(page)
            tree = pyLib.loadWeb(url).build_tree()
            for item in tree.xpath("//div[@class='tcat-main']//li/a[1]"):
                name = pyLib.stringify(item)
                href = item.get('href')
#                print name, href
                data.append({'name': name, 'url': href})
        return data
    
    def process(self, start=1, end=76):
        
        stories = self.getListStories(start, end)
        data = []
        for s in stories:
            if s == None: continue
            items = self.getDetailOfStories(s)
            for item in items:
                data.append(item)
                
        pyLib.gz_file_put_content(self._output + "/data", pyLib.encryptCipher(json.dumps(data)))
    
if __name__ == '__main__':
    
    
    a = VinaWap()
#    ss = a.getListStories()
#    for s in ss:
#        print s['name'], s['url']
    a.process(10, 14)
#    a.getDetailOfStories({'name': 'Mot Con Di', 'url': 'http://vinawap.mobi/story/ban-chat-cua-di/'})
    
    logging.info("finished")
    os._exit(1)
    